<?php
/**
 * Created by PhpStorm.
 * User: qincp
 * Date: 2019/03/14
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Channel\Models\ChannelDailyStat;
use App\Modules\Statistic\SendStatsEmailService;
use DB;
use Illuminate\Console\Command;
use Log;

class OfficialAccountDailyDataEmail extends Command
{
    /**
     * 服务号数据发送邮件
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'official_account_data_email';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '服务号数据发送邮件';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $date = date('Y-m-d', strtotime('-1 day'));
        $this->sendOfficialDailyDataEmail($date);
    }

    function sendOfficialDailyDataEmail($date)
    {
        $result = ChannelDailyStat::getListData($date, $date);
        $header = ['日期', '站点', '成本', '充值', '服务号名称', '主体', '累计关注', '新增关注', '取消关注', '取关率', '推广渠道', '代理商'];
        $data = array();

        $cost = 0;
        $amount_arry = [];
        $charge_amount = 0;
        $new_subscribe_num = 0;
        $total_subscribe_num = 0;
        $cancel_subscribe_num = 0;
        $distribution_channel_id_amount_arry = [];

        foreach ($result as $item) {
            $distribution_channel_id = $item->distribution_channel_id;
            if (isset($distribution_channel_id_amount_arry[$distribution_channel_id])) {
                $distribution_channel_id_amount_arry[$distribution_channel_id] += 1;
            } else {
                $distribution_channel_id_amount_arry[$distribution_channel_id] = 1;
            }
        }

        $content = "推广组数据后台服务号日报： <br/><br/><br/>
             <table border='1' cellpadding='10' cellspacing='0'>
                   <tr>
                       <td align='center' nowrap='nowrap'>日期</td>
                       <td align='center' nowrap='nowrap'>站点</td>
                       <td align='center' nowrap='nowrap'>成本</td>
                       <td align='center' nowrap='nowrap'>充值</td>
                       <td align='center' nowrap='nowrap'>服务号名称</td>
                       <td align='center' nowrap='nowrap'>主体</td>
                       <td align='center' nowrap='nowrap'>累计关注</td>
                       <td align='center' nowrap='nowrap'>新增关注</td>
                       <td align='center' nowrap='nowrap'>取消关注</td>
                       <td align='center' nowrap='nowrap'>取关率</td>
                       <td align='center' nowrap='nowrap'>推广渠道</td>
                       <td align='center' nowrap='nowrap'>代理商</td>
                   </tr>";

        foreach ($result as $item) {
            $new_subscribe_num += $item->new_subscribe_num;
            $total_subscribe_num += $item->total_subscribe_num;
            $cancel_subscribe_num += $item->cancel_subscribe_num;
            $distribution_channel_id = $item->distribution_channel_id;

            if (!isset($amount_arry[$distribution_channel_id])) {
                $cost += $item->cost;
                $charge_amount += $item->charge_amount;
                $amount_arry[$distribution_channel_id] = '111';
            }

            $data[] = array(
                $item->date,
                $item->distribution_channel_id,
                number_format($item->cost, 2),
                $item->charge_amount,
                $item->official_account_name,
                $item->company_name,
                $item->total_subscribe_num,
                $item->new_subscribe_num,
                $item->cancel_subscribe_num,
                $item->cancel_subscribe_rate,
                $item->promotion_point,
                $item->agent_name
            );

            $content .= "<tr>";
            if (isset($distribution_channel_id_amount_arry[$distribution_channel_id])) {
                $content .=
                    "<td align='center'nowrap='nowrap'rowspan=$distribution_channel_id_amount_arry[$distribution_channel_id]>{$item->date}</td>
                     <td align='center' nowrap='nowrap' rowspan=$distribution_channel_id_amount_arry[$distribution_channel_id]>$item->distribution_channel_id</td>
                     <td align='center' nowrap='nowrap' rowspan=$distribution_channel_id_amount_arry[$distribution_channel_id]>{$item->cost}</td>
                     <td align='center'nowrap='nowrap' rowspan=$distribution_channel_id_amount_arry[$distribution_channel_id]>{$item->charge_amount}</td>
                    ";
                unset($distribution_channel_id_amount_arry[$distribution_channel_id]);
            }
            $content .=
                "
                <td align='center' nowrap='nowrap'>{$item->official_account_name}</td>
                <td align='center' nowrap='nowrap'>{$item->company_name}</td>
                <td align='center' nowrap='nowrap'>{$item->total_subscribe_num}</td>
                <td align='center' nowrap='nowrap'>{$item->new_subscribe_num}</td>
                <td align='center' nowrap='nowrap'>{$item->cancel_subscribe_num}</td>
                <td align='center' nowrap='nowrap'>{$item->cancel_subscribe_rate}</td>
                <td align='center' nowrap='nowrap'>{$item->promotion_point}</td>
                <td align='center' nowrap='nowrap'>{$item->agent_name}</td>
           </tr>";
        }

        $content .=
            "<tr>
                <td align='center' nowrap='nowrap'><strong>总计</strong></td>
                <td align='center' nowrap='nowrap'></td>
                <td align='center' nowrap='nowrap'><strong>{$cost}</strong></td>
                <td align='center' nowrap='nowrap'><strong>{$charge_amount}</strong></td>
                <td align='center' nowrap='nowrap'></td>
                <td align='center' nowrap='nowrap'></td>
                <td align='center' nowrap='nowrap'><strong>{$total_subscribe_num}</strong></td>
                <td align='center' nowrap='nowrap'><strong>{$new_subscribe_num}</strong></td>
                <td align='center' nowrap='nowrap'><strong>{$cancel_subscribe_num}</strong></td>
                <td align='center' nowrap='nowrap'></td>
                <td align='center' nowrap='nowrap'></td>
                <td align='center' nowrap='nowrap'></td>
             </tr>";

        $content .= "</table><br/><br/><br/>详情请见附件 <br/><br/><br/>";

        $data[] = array('', '', '', '', '', '', '', '', '', '', '', '');
        $data[] = array(
            '总计',
            '',
            $cost,
            $charge_amount,
            '',
            '',
            $total_subscribe_num,
            $new_subscribe_num,
            $cancel_subscribe_num,
            '',
            '',
            ''
        );

        saveExcelData($header, $data, storage_path('app/official_account_daily.xlsx'));
        $to_user = array(
            ['address' => 'yuyy@yqsd.net', 'name' => '余艳艳'],
            ['address' => 'zhangzg@yqsd.net', 'name' => '张总'],
            ['address' => 'fangxq@yqsd.net', 'name' => 'fangxq@yqsd.net'],
            ['address' => 'wangqb@yqsd.net', 'name' => 'wangqb@yqsd.net'],
            ['address' => 'chenzt@yqsd.net', 'name' => 'chenzt@yqsd.net'],
            ['address' => 'chenbiao@zw88.net', 'name' => 'chenbiao@zw88.net'],
            ['address' => 'hanyh@yqsd.net', 'name' => 'hanyh@yqsd.net'],
            ['address' => 'zhangrl@yqsd.net', 'name' => 'zhangrl@yqsd.net'],
            ['address' => 'caicf@zw88.net', 'name' => 'caicf@zw88.net'],
            ['address' => 'qincp@iqiyoo.com', 'name' => '阿才'],
        );
        SendStatsEmailService::SendHtmlEmailWithAcce($to_user, ['subject' => "推广组数据后台服务号日报" . date("Y-m-d", time()), 'body' => $content], storage_path('app/official_account_daily.xlsx'));
    }
}